//
//  JFSelectArea.m
//  SelectDistrict
//
//  Created by JFL on 16/10/14.
//  Copyright © 2016年 JFL. All rights reserved.
//

#import "JFSelectAreaB.h"
#import "SelectAreaModel.h"


@interface JFSelectAreaB ()

{
    UIPickerView *pikerV;
}
@property (nonatomic ,strong)NSArray *provArr;
@property (nonatomic ,strong)NSArray *cityArr;
@property (nonatomic ,strong)NSArray *countyArr;
@property (nonatomic ,strong)NSMutableArray *dataArr;

@property (nonatomic ,strong)NSString *provName;
@property (nonatomic ,strong)NSString *cityName;
@property (nonatomic ,strong)NSString *countyName;
@property (nonatomic ,strong)NSString *addr_id;//县id
@property (nonatomic ,strong)NSString *addr_idP;//省id
@property (nonatomic ,strong)NSString *addr_idC;//市id

@property (nonatomic ,strong)NSArray *idArr;
@end
@implementation JFSelectAreaB
- (instancetype)initWithFrame:(CGRect)frame completeBlock:(CompleteBlock)block{
    self.areaBlock = block;
    [self loadDataAllProvice];
    if (self = [super initWithFrame:frame]) {
        UIView *maskV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, self.frame.size.height - 240)];
        maskV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];
        
        [self addSubview:maskV];
        [self createView];
        
    }
    return self;
}

- (void)createView{
    UIView *topV = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 240, self.frame.size.width, 35)];
    [self addSubview:topV];
    topV.backgroundColor = [UIColor whiteColor];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 0, 50, 35);
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:cancelBtn];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.frame = CGRectMake(self.frame.size.width - 50, 0, 50, 35);
    doneBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:doneBtn];
    
    pikerV = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 205, self.frame.size.width, 205)];
    pikerV.backgroundColor = [UIColor colorWithHexString:@"efefef"];
    pikerV.delegate = self;
    pikerV.dataSource = self;

    [self addSubview:pikerV];
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return self.provArr.count;
    }else if (component == 1){
        return self.cityArr.count;
    }else if (component == 2){
        return self.countyArr.count;
    }
    else{
        return 0;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    SelectAreaModel *model = nil;
    if (component == 0) {
        model = self.provArr[row];
        [self loadAllCity:model.CODE];
        [pikerV selectRow:0 inComponent:1 animated:YES];
        self.provName = model.NAME;
        self.cityName = nil;
        self.countyName = nil;
        
    }else if (component == 1){
        model = self.cityArr[row];
        [self loadAllCounty:model.CODE];
        [pikerV selectRow:0 inComponent:2 animated:YES];
        self.cityName = model.NAME;
        self.countyName = nil;
        
    }else if (component == 2){
        model = self.countyArr[row];
        self.countyName = model.NAME;
    }
    
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    
    UILabel *myView = nil;
    
    myView = [UILabel new];

    SelectAreaModel *model = nil;
    
    if (component == 0) {
        
        model = self.provArr[row];
        myView.text = model.NAME;
        
    }else if (component == 1){
        
        model = self.cityArr[row];
        myView.text = model.NAME;
        
    }else if (component == 2){
        
        model = self.countyArr[row];
        myView.text = model.NAME;
        
    }
    
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = [UIFont systemFontOfSize:13];
    return myView;
    
}
#pragma mark- 取消
- (void)cancelAction{
    [self removeFromSuperview];
}
#pragma mark- 完成
- (void)doneAction{
    if (!self.countyName) {
        [JKToast showWithText:@"请选择所在的省、市、县"];
        return;
    }

    [self removeFromSuperview];
    self.areaBlock([NSString stringWithFormat:@"%@;%@;%@",self.provName,self.cityName,self.countyName]);
}

#pragma mark- 所有省
- (void)loadDataAllProvice{
    
    NSString *urlStr = [NSString stringWithFormat:@"%@user/Personal/getProvince",kDomainName];

    BaseRequest *request =  [BaseRequest sharedBaseRequest];
    [request post:urlStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
            self.provArr = [SelectAreaModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [pikerV reloadAllComponents];
            SelectAreaModel *model = self.provArr[0];
            [self loadAllCity:model.CODE];
            
            SelectAreaModel *model1 = self.provArr[0];
            self.provName = model1.NAME;

            
        }else{
        
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
    }];
}

#pragma mark- 所有市
- (void)loadAllCity:(NSString *)proviceCode{
    
    if (proviceCode == nil) {
        [JKToast showWithText:@"请选择您所在的省份！"];
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@user/Personal/getCity",kDomainName];
    
    NSDictionary *paraDic = @{@"povince_code":proviceCode};
    
    BaseRequest *request =  [BaseRequest sharedBaseRequest];
    [request post:urlStr params:paraDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
            self.cityArr = [SelectAreaModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [pikerV reloadComponent:1];
            [pikerV selectRow:0 inComponent:2 animated:YES];
            SelectAreaModel *model = self.cityArr[0];
            [self loadAllCounty:model.CODE];
            
            SelectAreaModel *model1 = self.cityArr[0];
            self.cityName = model1.NAME;
            
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


#pragma mark- 所有县
- (void)loadAllCounty:(NSString *)cityCode{
    
    if (cityCode == nil) {
        [JKToast showWithText:@"请选择您所在的市！"];
        return;
    }
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@user/Personal/getTown",kDomainName];
    
    NSDictionary *paraDic = @{@"city_code":cityCode};
    
    BaseRequest *request =  [BaseRequest sharedBaseRequest];
    [request post:urlStr params:paraDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
            self.countyArr = [SelectAreaModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [pikerV reloadComponent:2];
            SelectAreaModel *model1 = self.countyArr[0];
            self.countyName = model1.NAME;
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


@end

